﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using DslamPort.Models;
namespace DslamPort.Controllers
{
    [Authorize]
    public class ReportController : Controller
    {
        //
        // GET: /Report/
        DonviModel DonviModel = new DonviModel();
        ReportModel ReportModel = new ReportModel();
        DslamPortModel DslamPortModel = new DslamPortModel(); 
        public ActionResult Index()
        {
            string s = "";
            if (Session["report_qh"] != null)
            {
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", Session["report_qh"]);
                int num;
                string quan_huyen_id;                
                ViewBag.selectedDonvi = "";
                if (int.TryParse(Session["report_qh"].ToString(), out num))
                {
                    quan_huyen_id = Session["report_qh"].ToString();
                    ViewBag.selectedDonvi = quan_huyen_id;
                    s = " and c.quan_huyen_id=" + quan_huyen_id;
                }                
            }
            else
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            List<ReportModel> DslamPort = ReportModel.getDslamPort(s);
            return View(DslamPort);
        }
        public static List<ReportModel> dslamport_global { get; set; }
        public static List<DslamPortModel> detail_dslamport_global { get; set; }
        public void Export_Excel(string type)
        {
            //var item = new System.Data.DataTable();

            //item.Columns.Add("Đơn vị", typeof(string));
            //item.Columns.Add("SYSTEM ID", typeof(string));
            //item.Columns.Add("Số cổng sử dụng", typeof(string));
            //item.Columns.Add("Số cổng trống", typeof(string));
            //item.Columns.Add("Số cổng hỏng", typeof(string));
            //for (int i = 0; i < dslamport_global.ToArray().Length; i++)
            //    item.Rows.Add(dslamport_global[i].ten_don_vi, dslamport_global[i].system_id, dslamport_global[i].sum_port_use, dslamport_global[i].sum_port_not_use, dslamport_global[i].sum_port_drop);
            //var grid = new GridView();
            //grid.DataSource = item;
            //grid.DataBind();
            //Response.ClearContent();
            //Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=bao_cao.xls");
            Response.ContentType = "application/ms-excel";
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            if (type == "0")
            {
                Response.Write("<h3>Báo cáo tổng quát PORT DSLAM</h3>");
                Response.Write("<BR>");
                Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
                Response.Write("<td><b>Đơn vị</b></td>");
                Response.Write("<td><b>System ID</b></td>");
                Response.Write("<td><b>Tên thiết bị</b></td>");
                Response.Write("<td><b>IP</b></td>");
                Response.Write("<td><b>Loại</b></td>");
                Response.Write("<td><b>Tổng số cổng</b></td>");
                Response.Write("<td><b>Số cổng sử dụng</b></td>");
                Response.Write("<td><b>Cổng dùng chung</b></td>");                
                Response.Write("<td><b>Số cổng trống</b></td>");
                Response.Write("<td><b>Số cổng hỏng</b></td>");
                Response.Write("</TR>");
                int sum = 0;
                for (int i = 0; i < dslamport_global.ToArray().Length; i++)
                {
                    sum = Convert.ToInt32(dslamport_global[i].sum_port_drop) + Convert.ToInt32(dslamport_global[i].sum_port_use) + Convert.ToInt32(dslamport_global[i].sum_port_not_use);
                    Response.Write("<TR>");
                    Response.Write("<td>" + dslamport_global[i].ten_don_vi + "</td>");
                    Response.Write("<td>" + dslamport_global[i].system_id + "</td>");
                    Response.Write("<td>" + dslamport_global[i].name + "</td>");
                    Response.Write("<td>" + dslamport_global[i].ip + "</td>");
                    if (dslamport_global[i].type_name == "5" || dslamport_global[i].type_name == "6")
                        Response.Write("<td>ATM</td>");
                    else
                        Response.Write("<td>IP</td>");
                    Response.Write("<td>" +  sum +"</td>");
                    Response.Write("<td>" + dslamport_global[i].sum_port_use + "</td>");
                    Response.Write("<td>" + dslamport_global[i].sum_two_port + "</td>");                    
                    Response.Write("<td>" + dslamport_global[i].sum_port_not_use + "</td>");
                    Response.Write("<td>" + dslamport_global[i].sum_port_drop + "</td>");
                    Response.Write("</TR>");
                }
                Response.Write("</Table>");
            }
            else {
                int dslam_type = 1;
                try
                {
                    if (detail_dslamport_global.First().rackid == "")
                        dslam_type = 0;
                }
                catch { }
                Response.Write("<h3>Báo cáo chi tiết PORT DSLAM</h3>");
                Response.Write("<BR>");
                Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");                
                Response.Write("<td><b>SYSTEM ID</b></td>");
                Response.Write("<td><b>Tên</b></td>");
                Response.Write("<td><b>Tình hình</b></td>");
                Response.Write("<td><b>Cổng</b></td>");
                if (dslam_type == 1)
                    Response.Write("<td><b>R/S | S/P | Vpi/Vci</b></td>");
                else
                    Response.Write("<td><b>S/P | Vpi/Vci</b></td>");
                Response.Write("<td><b>Account Internet</b></td>");
                Response.Write("<td><b>Gói DV Internet</b></td>");
                Response.Write("<td><b>Hình thức Internet</b></td>");
                Response.Write("<td><b>Account Mytv</b></td>");
                Response.Write("<td><b>Gói DV Mytv</b></td>");
                Response.Write("<td><b>Mã KH</b></td>");
                Response.Write("<td><b>Tên KH</b></td>");
                Response.Write("<td><b>Địa chỉ</b></td>");
                Response.Write("<td><b>Ngày ĐK</b></td>");
                Response.Write("</TR>");
                string dungluong = "";
                for (int i = 0; i < detail_dslamport_global.ToArray().Length; i++)
                {
                    Response.Write("<TR>");
                    Response.Write("<td>" + detail_dslamport_global[i].system_id + "</td>");
                    Response.Write("<td>" + detail_dslamport_global[i].name + "</td>");
                    if (detail_dslamport_global[i].status=="0")
                        Response.Write("<td style='background: yellow'>Trống</td>");
                    else if (detail_dslamport_global[i].status == "1")
                        Response.Write("<td>Đã dùng</td>");
                    else if (detail_dslamport_global[i].status == "2")
                        Response.Write("<td style='background: red'>Hỏng</td>");
                    if (detail_dslamport_global[i].dung_luong.Length > 0)
                        dungluong = " (" + detail_dslamport_global[i].dung_luong.Replace("DSL","") + ")";
                    Response.Write("<td>" + detail_dslamport_global[i].cong + dungluong + "</td>");
                    Response.Write("<td>");
                    if (dslam_type == 1)
                        Response.Write(detail_dslamport_global[i].rackid + "|" + detail_dslamport_global[i].selfid + "|");
                    Response.Write(detail_dslamport_global[i].slot + "|" + detail_dslamport_global[i].port + "|");
                    Response.Write(  detail_dslamport_global[i].vpi + "|" + detail_dslamport_global[i].vci );
                    Response.Write("</td>");
                    Response.Write("<td>" + detail_dslamport_global[i].account_int + "</td>");
                    Response.Write("<td>");
                    if (detail_dslamport_global[i].trang_thai_int=="0")
                        Response.Write(detail_dslamport_global[i].goidv_int + "/Khóa");
                    else if (detail_dslamport_global[i].trang_thai_int == "1")
                        Response.Write(detail_dslamport_global[i].goidv_int + "/Hoạt động");
                    else if (detail_dslamport_global[i].trang_thai_int == "2")
                        Response.Write(detail_dslamport_global[i].goidv_int + "/Tạm ngừng");
                    Response.Write("</td>");

                    Response.Write("<td>");
                    if (detail_dslamport_global[i].hinh_thuc_int == "0")
                        Response.Write("Lưu lượng");
                    else if (detail_dslamport_global[i].hinh_thuc_int == "1")
                        Response.Write("Trọn gói");
                    Response.Write("</td>");

                    Response.Write("<td>" + detail_dslamport_global[i].account_mytv + "</td>");
                    Response.Write("<td>");
                    if (detail_dslamport_global[i].trang_thai_mytv == "0")
                        Response.Write(detail_dslamport_global[i].goidv_mytv + "/Khóa");
                    else if (detail_dslamport_global[i].trang_thai_mytv == "1")
                        Response.Write(detail_dslamport_global[i].goidv_mytv + "/Hoạt động");
                    else if (detail_dslamport_global[i].trang_thai_mytv == "2")
                        Response.Write(detail_dslamport_global[i].goidv_mytv + "/Tạm ngừng");
                    Response.Write("</td>");
                    Response.Write("<td>" + detail_dslamport_global[i].ma_kh + "</td>");
                    Response.Write("<td>" + detail_dslamport_global[i].ten_kh + "</td>");
                    Response.Write("<td>" + detail_dslamport_global[i].diachi_kh + "</td>");
                    Response.Write("<td>" + detail_dslamport_global[i].ngay_dk_int + "</td>");
                    Response.Write("</TR>");
                }
                Response.Write("</Table>");
            }
            //Response.Charset = "";
            //StringWriter sw = new StringWriter();
            //HtmlTextWriter htw = new HtmlTextWriter(sw);

            //grid.RenderControl(htw);

            //Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
        public ActionResult FindReport()
        {
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult FindReport(string timkiem)
        {
            string quan_huyen_id = "";
            int num;
            string s = "";
            ViewBag.selectedDonvi = "";
            if (int.TryParse(Request.Form["quan_huyen_id"], out num))
            {
                quan_huyen_id = Request.Form["quan_huyen_id"];
                s = " and c.quan_huyen_id=" + quan_huyen_id;
                ViewBag.selectedDonvi = Request.Form["quan_huyen_id"];
            }
            else
                s = " ";
            List<ReportModel> DslamPort = ReportModel.getDslamPort(s);
            if (ViewBag.selectedDonvi.Length == 0)
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
            else
            {
                ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", ViewBag.selectedDonvi);                
            }
            dslamport_global = DslamPort;
            Session["report_qh"] = Request.Form["quan_huyen_id"]; 
            return View("Index", DslamPort);
        }
        public ActionResult DetailDslamPort(string status,string quan_huyen_id,string system_id,string name)
        {
            string s = "";
            if (status != "-1")
                if (status != "3")
                    s = " and a.status=" + status;
                else
                    s = "and a.account_int is not null and a.account_mytv is not null";
            string str = " and a.system_id='" + system_id + "' and c.quan_huyen_id=" + quan_huyen_id + s;
            List<DslamPortModel> DslamPort = DslamPortModel.getDslamPort(str);
            detail_dslamport_global = DslamPort;
            ViewBag.name = name;
            ViewBag.system_id = system_id;
            ViewBag.dslam_type = 1;
            try
            {
                if (DslamPort.First().rackid == "")
                    ViewBag.dslam_type = 0;
            }
            catch { }
            return View(DslamPort);
        }
    }
}
